package tmproject.hlhj.fhp.tmvote.customView

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.info_dialog.*
import tmproject.hlhj.fhp.tmvote.BaseAty
import tmproject.hlhj.fhp.tmvote.Content
import tmproject.hlhj.fhp.tmvote.R
import tmproject.hlhj.fhp.tmvote.beans.BaseBean
import tmproject.hlhj.fhp.tmvote.beans.VoteBean
import tmproject.hlhj.fhp.tmvote.network.Apis
import tmproject.hlhj.fhp.tmvote.network.BaseObersever
import tmproject.hlhj.fhp.tmvote.network.LoadingAction
import tmproject.hlhj.fhp.tmvote.utils.GlideUtils
import tmproject.hlhj.fhp.tmvote.utils.ToastUtil

/**
 * Created by Never Fear   on 2018\8\7 0007.
Never More....
 */
class InfoDialog(private val c: Context,private val bundle: Bundle,private val type:Int) :Dialog(c, R.style.MyDialog) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.info_dialog)
        if (type==0){
            img3.visibility=View.GONE
            lo2.visibility=View.GONE
            btVote.visibility=View.GONE
        }else{
            img3.visibility=View.VISIBLE
            lo2.visibility=View.VISIBLE
            btVote.visibility=View.VISIBLE
        }
        Glide.with(context).load(bundle.getString("img"))
                .apply(GlideUtils.loadOption())
                .into(itemImg)
        tvRank.text=bundle.getString("rank")
        itemName.text=bundle.getString("name")
        itemRank.text="NO.${bundle.getString("num")}"
        itemDes.text=bundle.getString("des")
        btClose.setOnClickListener {
            dismiss()
        }
        btVote.setBackgroundResource(Content.vote_commit_bottom_bac)
        btVote.setOnClickListener {
            Apis.vote(bundle.getString("id"),Content.UID)
                    ?.let {
                        it.doOnSubscribe(LoadingAction(c))
                                .subscribe(object :BaseObersever<Response<BaseBean>>(c){
                                    override fun onRespSuccess(t: Response<BaseBean>) {
                                        if ( c is BaseAty<*,*>){
                                            c.loadingDialog.dismiss()
                                            ToastUtil.toast("投票成功")
                                        }
                                    }
                                })
                    }
        }
    }
}